import{showdate,del,edit,isExituserName,isExituserPhone,updateuserinfo} from "./user.js";
var order="id"
var type="asc"
var con=""
var conInp=document.getElementsByClassName("searchCon")[0]
var orderInplist=document.getElementsByName("orderType")
var typeInplist=document.getElementsByName("order")
var content=document.getElementsByClassName("content")[0]
var search=document.getElementsByClassName("searchBtn")[0]
var shadow=document.getElementsByClassName("shadow")[0]
var editId=document.getElementsByClassName("editId")[0]
// var editUserId=document.getElementsByClassName("editUserId")[0]
var editUser=document.getElementsByClassName("editUser")[0]
var editPwd=document.getElementsByClassName("editPwd")[0]
var editPhone=document.getElementsByClassName("editPhone")[0]
var editSub=document.getElementsByClassName("editSub")[0]
var editCan=document.getElementsByClassName("editCan")[0]
conInp.onchange=function(){
    con=conInp.value
}
for(var i=0;i<orderInplist.length;i++){
    orderInplist[i].onchange=function(){
        order=this.value
        console.log(order)
        updatepage()
    }
}
for(var i=0;i<typeInplist.length;i++){
    typeInplist[i].onchange=function(){
        type=this.value
        console.log(type)
        updatepage()
    }
}
search.onclick=function(){
    updatepage()
}
updatepage()

function updatepage(){
    showdate(con,order,type).then(function(result){
    var html=""
    result.forEach(item => {
            html +=`<li>
                    <span>${item.id}</span>
                    <span>${item.id}${item.user_name}</span>
                    <span>${item.user_name}</span>
                    <span>${item.user_pwd}</span>
                    <span>${item.user_phone}</span>
                    <span edit_id=${item.id} class="edit">编辑</span>
                    <span delete_id=${item.id} class="delete">删除</span>
                </li>`
        });
    content.innerHTML=html 
    active()
})
}


function active(){
    var editlist=document.getElementsByClassName("edit")
    var deletelist=document.getElementsByClassName("delete")
    // 删除
    for(var i=0;i<deletelist.length;i++){
         deletelist[i].onclick=function(){
            var id=this.getAttribute("delete_id")
            this.parentElement.remove()
            del(id).then(function(a){
                alert(a.status)
            })
         }
      
    }
    for(var i=0;i<editlist.length;i++){
        editlist[i].onclick=function(){
          shadow.style.display="block"
          var id=this.getAttribute("edit_id")
          edit(id).then(function(e){
            editId.innerHTML=e[0].id
            editUser.value=e[0].user_name
            editPwd.value=e[0].user_pwd
            editPhone.value=e[0].user_phone
          })
        editSub.onclick=function(){
            Promise.all([isExituserName(editUser.value),isExituserPhone(editPhone.value)]).then(function(e){
                console.log(e)
                if(e.every(item=>item.status==true)){
                    var user_name=editUser.value
                    var user_pwd=editPwd.value
                    var user_phone=editPhone.value
                    updateuserinfo(user_name,user_pwd,user_phone,id).then(function(e){
                        updatepage()
                        shadow.style.display="none"
                          alert(e.status)
                    })
                }
                else{
                    var res=e.find(item=>item.status==false)
                    alert(res.msg)
                }
            })
        }

        }
    }
}